<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use app\admin\model\Admin as AdminModel;

class Pubctrl extends Controller
{
    public function login()
    {
        return $this->fetch();
    }

    // 验证码检测
    public function check(Request $request)
    {
        $result  = $this->validate($request->param(),[
            'username|用户名'=>'require',
            'password|用户密码'=>'require',
            'yzm|验证码'=>'require|captcha',        
        ]);

        if (true !== $result) {
            $this->error($result);
        }

        $username = $request->param('username');
        $password = $request->param('password');

        //$username = strtolower($username0);
        //$password = strtolower($password0);

        $admin = AdminModel::getByUsername($username);
        if($admin){
            if($password == $admin->password){
                session('id',$admin->id);
                session('username',$admin->username);
                session('role_id',1);
                //跳转
                //$this -> success('登录成功！',url('Index/index'));
                $this->redirect('admin/index/index');
            }else{
                //密码错误
                $this -> error('用户名或密码错误...');
            }
        }

        //什么也不是
        $this -> error('用户名或密码错误...');
    }

    
    //退出方法 
    public function logout(){
        //清除session
        session(null);
        //跳转到登录页面
        //$this -> success('退出成功',url('login'),3);
        $this->redirect('admin/pubctrl/login');exit;
    }

}
